package com.ksy.common.view;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ksy.common.R;
import com.ksy.common.utils.LogTool;


/**
 * Created by Ksy.
 */

public class ProxyLayout<T extends View> extends PullToRefreshLayout {

    private EmptyView emptyView;
    private ErrorView errorView;
    private RelativeLayout contentView;
    private LoadingView loadingView;


    public ProxyLayout(Context context, T targetView) {
        super(context);
        RelativeLayout rootLayout = new RelativeLayout(getContext());
        addView(rootLayout, getParams());
        setEnableAutoLoadmore(false);
        setBackgroundResource(R.color.common_colorBg);
        contentView = new RelativeLayout(getContext());
        emptyView = new EmptyView(getContext());
        errorView = new ErrorView(getContext());
        loadingView = new LoadingView(getContext());
        showContentView();
        ViewGroup parentViewGroup = (ViewGroup) targetView.getParent();
        int index;
        if (parentViewGroup != null) {
            parentViewGroup.removeView(targetView);
            index = parentViewGroup.indexOfChild(targetView);
            parentViewGroup.addView(this, index);
        }
        ViewGroup.LayoutParams layoutParams = targetView.getLayoutParams();
        if (layoutParams == null) {
            LogTool.e("layoutParams == null");
            layoutParams = getParams();
        }
        targetView.setLayoutParams(getParams());
        this.setLayoutParams(layoutParams);
        contentView.addView(targetView);
        rootLayout.addView(loadingView, getParams());
        rootLayout.addView(emptyView, getParams());
        rootLayout.addView(contentView, getParams());
        rootLayout.addView(errorView, getParams());

    }

    public void addContentView(View view, ViewGroup.LayoutParams layoutParams) {
        if (contentView != null) {
            contentView.addView(view, layoutParams);
        }
    }

    public void addContentView(View view) {
        if (contentView != null) {
            contentView.addView(view);
        }
    }


    public void showNoneView() {
        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (contentView != null) {
            contentView.setVisibility(GONE);
        }
    }

    public void showLoadingView() {
        if (loadingView != null) {
            loadingView.setVisibility(VISIBLE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (contentView != null) {
            contentView.setVisibility(GONE);
        }
    }

    public void showEmptyView() {

        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(VISIBLE);
        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (contentView != null) {
            contentView.setVisibility(GONE);
        }
    }

    public void showErrorView() {

        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(VISIBLE);
        }
        if (contentView != null) {
            contentView.setVisibility(GONE);
        }
    }

    public void showContentView() {

        if (loadingView != null) {
            loadingView.setVisibility(GONE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(GONE);
        }
        if (errorView != null) {
            errorView.setVisibility(GONE);
        }
        if (contentView != null) {
            contentView.setVisibility(VISIBLE);
        }
    }


    private ViewGroup.LayoutParams getParams() {
        return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }
}
